package top.shiyiri.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Aunean
 * @date 2022/1/5
 */
/*
JDK5.0 foreach 循环
 */
public class ForeachTest {

    Collection<Object> list = new ArrayList<>();
    {
        list.add(123);
        list.add(324);
        list.add(new String("Tom"));
        list.add(false);
        list.add(new Person("Jerry", 20));
        Person anna = new Person("Anna", 23);
        list.add(anna);
    }

    @Test
    public void test() {
        //for(集合元素的类型 局部变量 : 集合对象)
        //内部仍然调用迭代器
        for (Object obj: list) {
            System.out.println(obj);
        }
    }

    @Test
    public void test2() {

        String[] arr = new String[]{"MM", "MM", "MM"};

        for (int i = 0; i < arr.length; i++) {
            arr[i] = "GG";
        }

        for (String str: arr) {
            System.out.println(str);
        }
    }
}
